home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / GFCLIENT.PY < prev    next >
Encoding:
Python Source  |  2000-07-12  |  6.6 KB  |  218 lines

  1.  
  2. """client access to gadfly server. (gfserve.py)
  3.  
  4. Imported as a module this module provides interfaces
  5. that remotely access a gadfly database server.
  6.  
  7. Remote connections: gfclient
  8.  
  9.    connection = gfclient.gfclient(
  10.       policy,  # the name of the connection policy ["admin" for admin]
  11.       port,    # the port number the server is running on
  12.       password,# the password of the policy
  13.       [machine]) # (optional) the machine where server is running
  14.                  # (defaults to localhost)
  15.  
  16.    methods for gfclient connections:
  17.       gfclient.checkpoint() checkpoint the server (fails silently
  18.           if connection is not "admin").
  19.       gfclient.restart() restart the server (fails silently
  20.           if connection is not "admin").
  21.       gfclient.shutdown() shutdown the server (fails silently
  22.           if connection is not "admin").
  23.       cursor = gfclient.cursor() returns a cursor on this connection
  24.  
  25.    methods for cursor objects:
  26.  
  27.       cursor.execute(statement, dynamic_parameters=None)
  28.           execute the statement with optional dynamic parameters.
  29.           Dynamic parameters can be a list of tuples for INSERT
  30.           VALUES statements, otherwise they must be a tuple
  31.           of values.
  32.       cursor.execute_prepared(name, dynamic_parameters=None)
  33.           execute a named statement configured for this connection
  34.           policy, with optional dynamic parameters. Dynamic
  35.           parameters permitted as for execute depending on the
  36.           statement the name refers to.
  37.       cursor.fetchall()
  38.           return results of the last executed statement
  39.           (None for non-queries, or list of tuples).
  40.  
  41. See gfstest.py for example usage.
  42.      
  43. SCRIPT INTERPRETATION:
  44.  
  45. Server maintenance utilities
  46.  
  47. COMMAND LINE:
  48.    python gfclient.py action port admin_password [machine]
  49.  
  50. TEST EXAMPLE:
  51.    python gfclient.py shutdown 2222 admin
  52.  
  53.    action: one of
  54.       shutdown:  shut down the server with no checkpoint
  55.       restart: restart the server (re-read the database and recover)
  56.       checkpoint: force a database checkpoint now
  57.    port: the port the server is running on
  58.    admin_password: the administrative password for the server
  59.    machine: [optional] the machine the server runs on.
  60. """
  61.  
  62. import gfsocket
  63.  
  64. def main():
  65.     import sys
  66.     try:
  67.         done=0
  68.         argv = sys.argv
  69.         [action, port, admin_password] = argv[1:4]
  70.         from string import atoi
  71.         port = atoi(port)
  72.         if len(argv)>4:
  73.            machine = argv[4]
  74.         else:
  75.            machine = None
  76.         print action, port, admin_password, machine
  77.         if action not in ["shutdown", "restart", "checkpoint"]:
  78.            print "bad action", action
  79.            print
  80.            return
  81.         dosimple(action, port, admin_password, machine)
  82.         done=1
  83.     finally:
  84.         if not done:
  85.             print __doc__
  86.  
  87. def dosimple(action, port, pw, machine=None):
  88.     import socket
  89.     if machine is None:
  90.        machine = socket.gethostname()
  91.     conn = gfclient("admin", port, pw, machine)
  92.     action = getattr(conn, action)
  93.     print action()
  94.  
  95. # copied from gfserve
  96. # shut down the server (admin policy only)
  97. #   arguments = ()
  98. #   shutdown the server with no checkpoint
  99. SHUTDOWN = "SHUTDOWN"
  100.  
  101. # restart the server (admin only)
  102. #   arguments = ()
  103. #   restart the server (recover)
  104. #   no checkpoint
  105. RESTART = "RESTART"
  106.  
  107. # checkpoint the server (admin only)
  108. #   arguments = ()
  109. #   checkpoint the server
  110. CHECKPOINT = "CHECKPOINT"
  111.  
  112. # exec prepared statement
  113. #   arguments = (prepared_name_string, dyn=None)
  114. #   execute the prepared statement with dynamic args.
  115. #   autocommit.
  116. EXECUTE_PREPARED = "EXECUTE_PREPARED"
  117.  
  118. # exec any statement (only if not disabled)
  119. #   arguments = (statement_string, dyn=None)
  120. #   execute the statement with dynamic args.
  121. #   autocommit.
  122. EXECUTE_STATEMENT = "EXECUTE_STATEMENT"
  123.  
  124. class gfclient:
  125.  
  126.     closed = 0
  127.     
  128.     def __init__(self, policy, port, password, machine=None):
  129.         import socket
  130.         self.policy = policy
  131.         self.port = port
  132.         self.password = password
  133.         if machine is None:
  134.             machine = socket.gethostname()
  135.         self.machine = machine
  136.         
  137.     def open_connection(self):
  138.         import socket
  139.         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  140.         #print type(sock), sock
  141.         sock.connect((self.machine, self.port))
  142.         return sock
  143.         
  144.     def send_action(self, action, arguments, socket):
  145.         gfsocket.send_certified_action(
  146.           self.policy, action, arguments, self.password, socket)
  147.         
  148.     def checkpoint(self):
  149.         return self.simple_action(CHECKPOINT)
  150.     
  151.     def simple_action(self, action, args=()):
  152.         """only valid for admin policy: force a server checkpoint"""
  153.         sock = self.open_connection()
  154.         self.send_action(action, args, sock)
  155.         data = gfsocket.recv_data(sock)
  156.         data = gfsocket.interpret_response(data)
  157.         return data
  158.     
  159.     def restart(self):
  160.         """only valid for admin policy: force a server restart"""
  161.         return self.simple_action(RESTART)
  162.         
  163.     def shutdown(self):
  164.         """only valid for admin policy: shut down the server"""
  165.         return self.simple_action(SHUTDOWN)
  166.     
  167.     def close(self):
  168.         self.closed = 1
  169.         
  170.     def commit(self):
  171.         # right now all actions autocommit
  172.         pass
  173.         
  174.     # cannot rollback, autocommit on success
  175.     rollback = commit
  176.     
  177.     def cursor(self):
  178.         """return a cursor to this policy"""
  179.         if self.closed:
  180.             raise ValueError, "connection is closed"
  181.         return gfClientCursor(self)
  182.         
  183.  
  184. class gfClientCursor:
  185.  
  186.     statement = None
  187.     results = None
  188.     description = None
  189.  
  190.     def __init__(self, connection):
  191.         self.connection = connection
  192.         
  193.     # should add fetchone fetchmany
  194.     def fetchall(self):
  195.         return self.results
  196.     
  197.     def execute(self, statement=None, params=None):
  198.         con = self.connection
  199.         data = con.simple_action(EXECUTE_STATEMENT, (statement, params))
  200.         (self.description, self.results) = data
  201.     
  202.     def execute_prepared(self, name, params=None):
  203.         con = self.connection
  204.         data = con.simple_action(EXECUTE_PREPARED, (name, params))
  205.         if data is None:
  206.             self.description = self.results = None
  207.         else:
  208.             (self.description, self.results) = data
  209.     
  210.     def setoutputsizes(self, *args):
  211.         pass # not implemented
  212.         
  213.     def setinputsizes(self):
  214.         pass # not implemented
  215.         
  216. if __name__=="__main__":
  217.     main()
  218.